/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package cn.fengkuok.core.workflow.jbpm;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.jbpm.api.identity.Group;
import org.jbpm.api.identity.User;
import org.jbpm.pvm.internal.env.BasicEnvironment;
import org.jbpm.pvm.internal.identity.impl.GroupImpl;
import org.jbpm.pvm.internal.identity.spi.IdentitySession;
import org.jbpm.pvm.internal.util.CollectionUtil;

public class IdentitySessionImpl implements IdentitySession {
	private Session session;
	String queryOperatorRoleSql = "from Role where roleId in (select role from OperatorRole where operator = ?)";
	String queryPositionRoleSql = "from Role where roleId in (select role from PositionRole where position = ?)";
	public IdentitySessionImpl() {
		this.session = BasicEnvironment.getFromCurrent(Session.class);
	}

	public String createUser(String userName, String givenName, String familyName, String businessEmail) {
		return null;
	}

	public User findUserById(String userId) {
//		final Operator operator = (Operator) session.createCriteria(Operator.class)
//				.add(Restrictions.eq("loginName", userId)).uniqueResult();
//		final Employee employee = (Employee) session.createCriteria(Employee.class)
//				.add(Restrictions.eq("operatorId", operator.getOperatorId())).uniqueResult();
//		User user = new User() {
//			@Override
//			public String getId() {
//				return operator.getLoginName();
//			}
//
//			@Override
//			public String getGivenName() {
//				return employee.getFirstName();
//			}
//
//			@Override
//			public String getFamilyName() {
//				return employee.getLastName();
//			}
//
//			@Override
//			public String getBusinessEmail() {
//				return employee.getOemail();
//			}
//		};
//		return user;
		return null;
	}

	public List<User> findUsersById(String... userIds) {
//		List<User> userList = new ArrayList<User>();
//		List<Operator> operatorList = session.createCriteria(Operator.class).add(Restrictions.in("loginName", userIds)).list();
//		for (final Operator operator : operatorList) {
//			final Employee employee = (Employee) session.createCriteria(Employee.class).add(Restrictions.eq("operatorId", operator.getOperatorId())).uniqueResult();
//			User user = new User() {
//				@Override
//				public String getId() {
//					return operator.getLoginName();
//				}
//
//				@Override
//				public String getGivenName() {
//					return employee.getFirstName();
//				}
//
//				@Override
//				public String getFamilyName() {
//					return employee.getLastName();
//				}
//
//				@Override
//				public String getBusinessEmail() {
//					return employee.getOemail();
//				}
//			};
//			userList.add(user);
//		}
//		
//		
//		return userList;
		return null;
	}

	public List<User> findUsers() {
//		List<Operator> users = session.createCriteria(Operator.class).list();
//		for (final Operator operator : users) {
//			final Employee employee = (Employee) session.createCriteria(Employee.class)
//					.add(Restrictions.eq("operatorId", operator.getOperatorId())).uniqueResult();
//			User user = new User() {
//				@Override
//				public String getId() {
//					return operator.getLoginName();
//				}
//
//				@Override
//				public String getGivenName() {
//					return employee.getFirstName();
//				}
//
//				@Override
//				public String getFamilyName() {
//					return employee.getLastName();
//				}
//
//				@Override
//				public String getBusinessEmail() {
//					return employee.getOemail();
//				}
//			};
//		}
//		return CollectionUtil.checkList(users, User.class);
		return null;
	}

	public void deleteUser(String userId) {
	}

	public String createGroup(String groupName, String groupType, String parentGroupId) {
		return null;
	}

	public List<User> findUsersByGroup(String groupId) {
		return null;
	}

	public GroupImpl findGroupById(String groupId) {
		return null;
	}

	public List<Group> findGroupsByUserAndGroupType(String userId, String groupType) {
		return null;
	}
	public List<Group> findGroupsByUser(String userId) {
//		List<Group> groups = new ArrayList<Group>();
//		final Operator operator = (Operator) session.createCriteria(Operator.class).add(Restrictions.eq("loginName", userId)).uniqueResult();
//		final Employee employee = (Employee) session.createCriteria(Employee.class).add(Restrictions.eq("operatorId", operator.getOperatorId())).uniqueResult();
//
//		final Position position = (Position) session.createCriteria(Position.class).add(Restrictions.eq("positionId", employee.getPositionId())).uniqueResult();
//		final Organization organization = (Organization) session.createCriteria(Organization.class).add(Restrictions.eq("orgId", employee.getOrgId())).uniqueResult();
//		
//		List<Role> operatorRoleList = session.createQuery(queryOperatorRoleSql).setParameter(0, operator).list();
//		List<Role> positionRoleList = session.createQuery(queryPositionRoleSql).setParameter(0, position).list();
//		
//		for (final Role role : positionRoleList) {
//			Group positionRoleGroup = new Group() {
//				@Override
//				public String getType() {
//					return "role";
//				}
//
//				@Override
//				public String getName() {
//					return role.getRoleName();
//				}
//
//				@Override
//				public String getId() {
//					return "r_" + role.getRoleName();
//				}
//			};
//			groups.add(positionRoleGroup);
//		}
//		for (final Role role : operatorRoleList) {
//			Group operatorRoleGroup = new Group() {
//				@Override
//				public String getType() {
//					return "role";
//				}
//
//				@Override
//				public String getName() {
//					return role.getRoleName();
//				}
//
//				@Override
//				public String getId() {
//					return "r_" + role.getRoleName();
//				}
//			};
//			groups.add(operatorRoleGroup);
//		}
//		Group positionGroup = new Group() {
//			@Override
//			public String getType() {
//				return "position";
//			}
//
//			@Override
//			public String getName() {
//				return position.getPositionName();
//			}
//
//			@Override
//			public String getId() {
//				return "p_" + position.getPositionName();
//			}
//		};
//		Group organizationGroup = new Group() {
//			@Override
//			public String getType() {
//				return "organization";
//			}
//
//			@Override
//			public String getName() {
//				return organization.getOrgName();
//			}
//
//			@Override
//			public String getId() {
//				return "o_" + organization.getOrgName();
//			}
//		};
//		groups.add(positionGroup);
//		groups.add(organizationGroup);
//		return groups;
		return null;
	}

	public List<Group> findGroups() {
		return null;
	}

	public void deleteGroup(String groupId) {
	}

	public void createMembership(String userId, String groupId, String role) {
	}

	public void deleteMembership(String userId, String groupId, String role) {
	}

	public void setSession(Session session) {
		this.session = session;
	}

}
